<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%--在线考试保存答卷--%>
<%
    JSONObject jsonData = new JSONObject();
    RecordSet rs = new RecordSet();
    BaseBean baseBean = new BaseBean();
    String formtable = "formtable_main_286";//在线考试答题
    String requestid = Util.null2String(request.getParameter("requestid"));
    baseBean.writeLog(">>>>saveTest_Ajax>>>requestid="+requestid);
    String mainid = getFieldValByname("id", requestid, formtable);
    baseBean.writeLog(">>>>saveTest_Ajax>>>mainid="+mainid);
    String result = Util.null2String(request.getParameter("result"));
    baseBean.writeLog(">>>>saveTest_Ajax>>>result="+result);
    updateDtsc(requestid, formtable);
    JSONObject resultJSON = JSONObject.parseObject(result);
    //填写的单选题
    JSONArray dxtList = JSONArray.parseArray(resultJSON.getString("dxt"));
    if (dxtList.size() > 0) {
        for (int i = 0; i < dxtList.size(); i++) {
            int xh = Util.getIntValue(dxtList.getJSONObject(i).getString("xh").substring(2)) + 1;
            baseBean.writeLog(">>>>saveTest_Ajax>>>单选题>>>xh="+xh);
            String txnr = dxtList.getJSONObject(i).getString("txnr");
            rs.executeUpdate("update " + formtable + "_dt1 set txnr='" + txnr + "' where mainid=? and px=?", mainid, xh);
        }
    }
    //填写的多选题
    JSONArray duoxtList = JSONArray.parseArray(resultJSON.getString("duoxt"));
    if (duoxtList.size() > 0) {
        for (int i = 0; i < duoxtList.size(); i++) {
            int xh = Util.getIntValue(duoxtList.getJSONObject(i).getString("xh").substring(2)) + 1;
            baseBean.writeLog(">>>>saveTest_Ajax>>>多选题>>>xh="+xh);
            String txnr = duoxtList.getJSONObject(i).getString("txnr");
            rs.executeUpdate("update " + formtable + "_dt2 set txnr='" + txnr + "' where mainid=? and px=?", mainid, xh);
        }
    }
    //填写的判断题
    JSONArray pdtList = JSONArray.parseArray(resultJSON.getString("pdt"));
    if (pdtList.size() > 0) {
        for (int i = 0; i < pdtList.size(); i++) {
            int xh = Util.getIntValue(pdtList.getJSONObject(i).getString("xh").substring(2)) + 1;
            baseBean.writeLog(">>>>saveTest_Ajax>>>判断题>>>xh="+xh);
            String txnr = pdtList.getJSONObject(i).getString("txnr");
            rs.executeUpdate("update " + formtable + "_dt3 set txnr='" + txnr + "' where mainid=? and px=?", mainid, xh);
        }
    }
    //填写的简答题
    JSONArray jdtList = JSONArray.parseArray(resultJSON.getString("jdt"));
    if (jdtList.size() > 0) {
        for (int i = 0; i < jdtList.size(); i++) {
            int xh = Util.getIntValue(jdtList.getJSONObject(i).getString("xh").substring(2)) + 1;
            baseBean.writeLog(">>>>saveTest_Ajax>>>简答题>>>xh="+xh);
            String txnr = jdtList.getJSONObject(i).getString("txnr");
            rs.executeUpdate("update " + formtable + "_dt4 set txnr='" + txnr + "' where mainid=? and px=?", mainid, xh);
        }
    }

    jsonData.put("status", true);
    out.print(jsonData.toJSONString());
%>

<%!
    /**
     * 获取表单字段
     */
    public String getFieldValByname(String filename, String requestid, String formtable) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select " + filename + " from " + formtable + " where requestid=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 更新答题时长
     */
    public static void updateDtsc(String requestid, String formtable) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        rs.executeQuery("select ksdjsj,isnull(sfbc,1) sfbc from " + formtable + " where requestid=?", requestid);
        rs.next();
        String ksdjsj = Util.null2String(rs.getString("ksdjsj"));
        String sfbc = Util.null2String(rs.getString("sfbc"));
        String sql = "";
        baseBean.writeLog(">>>>getWriteTest.jsp>>>ksdjsj="+ksdjsj+",sfbc="+sfbc);
        if ("1".equals(sfbc)) {
            String fullDate = DateUtil.getFullDate();
            long timeInterval = DateUtil.timeInterval(ksdjsj, fullDate);
            String dtsc = secondConvertHourMinSecond(timeInterval);
            sql = "update " + formtable + " set sfbc=0,djjssjfz='" + fullDate + "',dtsc='" + dtsc + "' where requestid=?";
            rs.executeUpdate(sql, requestid);
            sql = "update uf_kstjtz_dt1 set sjtjsj='" + fullDate + "',dtscfz='" + dtsc + "' where sjxq=?";
            rs.executeUpdate(sql, requestid);

        }
    }

    /**
     * 将秒转为时分秒格式【01:01:01】
     */
    public static String secondConvertHourMinSecond(Long second) {
        String str = "00时00分00秒";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + "时") : "00时";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + "分";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s) + "秒";
        return str;
    }
%>




